﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Core;
using Newtonsoft.Json;
using System.Runtime.InteropServices;

namespace WinClock
{
    public partial class Clock : Form
    {
        /// <summary>
        /// 软件名称
        /// </summary>
        public string softName = "MainClock";
        /// <summary>
        /// 获取当前路径
        /// </summary>
        public string projectPath = System.Environment.CurrentDirectory;
        /// <summary>
        /// 获取服务器时间URL
        /// </summary>
        public string urlTime = "https://f.m.suning.com/api/ct.do";
        /// <summary>
        /// 单例
        /// </summary>
        private static Clock instance;
        private static object _lock = new object();
        /// <summary>
        /// 
        /// </summary>
        private bool dragging = false;
        private Point dragCursorPoint;
        private Point dragFormPoint;
        public Clock()
        {
            InitializeComponent();
            // windows 分辨率大小
            int winWidth = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
            int winHeight = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
            // 设置WinForms窗口大小
            Size size = new Size(313, 114);
            this.Size = size;
            this.Text = softName;
            // 布局组件
            this.panelBox.MouseDown += new MouseEventHandler(panelBox_MouseDown);
            this.panelBox.MouseMove += new MouseEventHandler(panelBox_MouseMove);
            this.panelBox.MouseUp += new MouseEventHandler(panelBox_MouseUp);
            // 时间显示组件
            this.lblTime.MouseDown += new MouseEventHandler(lblTime_MouseDown);
            this.lblTime.MouseMove += new MouseEventHandler(lblTime_MouseMove);
            this.lblTime.MouseUp += new MouseEventHandler(lblTime_MouseUp);
        }
        /// <summary>
        /// 单例
        /// </summary>
        /// <returns></returns>
        public static Clock GetInstance()
        {
            if (instance == null || instance.IsDisposed)
            {
                lock (_lock)
                {
                    if (instance == null || instance.IsDisposed)
                    {
                        instance = new Clock();
                    }
                }
            }
            return instance;
        }
        /// <summary>
        /// 
        /// </summary>
        private void loadTime()
        {
            string j = HttpHelper.Get(this.urlTime);
            Object obj = JsonConvert.DeserializeObject(j);
            Newtonsoft.Json.Linq.JObject js = obj as Newtonsoft.Json.Linq.JObject;//把上面的obj转换为 Jobject对象
            string time = (Convert.ToInt64(js["currentTime"].ToString()) / 1000).ToString();
            DateTime dt = DateTimeHelper.GetTimeFromUint(time);
            this.lblTime.Text = dt.ToString("HH:mm:ss");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public Font ShowFont(float size)
        {
            Font font = null;
            System.Drawing.Text.PrivateFontCollection privateFonts = new System.Drawing.Text.PrivateFontCollection();
            string fontsPath = projectPath + @"\fonts\YournameD7HomeHalf.ttf";
            privateFonts.AddFontFile(fontsPath);
            font = new Font(privateFonts.Families[0], size);
            return font;
        }
        private void timerTime_Tick(object sender, EventArgs e)
        {
            this.lblTime.Font = ShowFont(60);
            loadTime();
        }

        private void panelBox_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                dragging = true;
                dragCursorPoint = Cursor.Position;
                dragFormPoint = this.Location;
            }
        }

        private void panelBox_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                dragging = false;
            }
        }

        private void panelBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (dragging)
            {
                Point dif = Point.Subtract(Cursor.Position, new Size(dragCursorPoint));
                this.Location = Point.Add(dragFormPoint, new Size(dif));
            }
        }

        private void lblTime_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                dragging = true;
                dragCursorPoint = Cursor.Position;
                dragFormPoint = this.Location;
            }
        }

        private void lblTime_MouseMove(object sender, MouseEventArgs e)
        {
            if (dragging)
            {
                Point dif = Point.Subtract(Cursor.Position, new Size(dragCursorPoint));
                this.Location = Point.Add(dragFormPoint, new Size(dif));
            }
        }

        private void lblTime_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                dragging = false;
            }
        }

        private void Clock_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                dragging = true;
                dragCursorPoint = Cursor.Position;
                dragFormPoint = this.Location;
            }
        }

        private void Clock_MouseMove(object sender, MouseEventArgs e)
        {
            if (dragging)
            {
                Point dif = Point.Subtract(Cursor.Position, new Size(dragCursorPoint));
                this.Location = Point.Add(dragFormPoint, new Size(dif));
            }
        }

        private void Clock_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                dragging = false;
            }
        }
    }
}
